package cn.scu.ne04jdemo.controller;


import cn.scu.ne04jdemo.entitiy.InterfaceInfoEntity;
import cn.scu.ne04jdemo.service.InterfaceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/interface/info")
public class InterfaceInfoController{

    @Autowired
    private InterfaceInfoService interfaceInfoService;


    @GetMapping("/findPage")
    public IPage<InterfaceInfoEntity> findPage(@RequestParam Integer id, @RequestParam String interfaceType
            , @RequestParam(required = true) long current, @RequestParam(required = true)  long size){
        QueryWrapper<InterfaceInfoEntity> params = new QueryWrapper();
        params.eq(id != null, "id", id)
                .eq(StringUtils.isNotBlank(interfaceType), "interface_type", interfaceType);
        IPage<InterfaceInfoEntity> page = new Page<InterfaceInfoEntity>(current, size);
        try {
            page = interfaceInfoService.page(page, params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return page;
    }



    @PostMapping("/delById")
    public Boolean delById(@RequestParam(required = true) Integer id){
        try {
            interfaceInfoService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @PostMapping("/forbidById")
    public Boolean forbidById(@RequestParam(required = true) Integer id){
        try {
            InterfaceInfoEntity interfaceInfoEntity = new InterfaceInfoEntity();
            interfaceInfoEntity.setId(id);
            interfaceInfoEntity.setInterfaceState("禁用");
            interfaceInfoService.updateById(interfaceInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @PostMapping("/save")
    @ResponseBody
    public Boolean save(@RequestBody InterfaceInfoEntity interfaceInfoEntity){
        try {
            if(!ObjectUtils.isNotNull(interfaceInfoEntity))
                interfaceInfoEntity = new InterfaceInfoEntity();
            interfaceInfoService.save(interfaceInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
